//
// StoreNuke.Net -  http://www.storenuke.net
// Copyright (c) 2005
// by Robert J Collins ( info@storenuke.net ) of WillowTree Software, Inc. ( http://www.willowtreesoftware.com )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace StoreNuke.StarterKit.Commerce
{
	/// <summary>
	/// Summary description for StoreGlobalDB.
	/// </summary>
	public class StoreGlobalDB
	{
		public string ExecuteSQLScript(string strScript) 
		{ 
			string strSQLExceptions = ""; 
			strScript = strScript.Replace("GO","|");
			string[] arrSQL = strScript.Split(Convert.ToChar("|")); 
			foreach (string strSQL in arrSQL) 
			{ 
				SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); 
				SqlCommand myCommand = new SqlCommand(); 
				myCommand.Connection = myConnection; 
				myCommand.CommandType = CommandType.Text; 
				myCommand.CommandText = strSQL; 
				myConnection.Open(); 
				try 
				{ 
					myCommand.ExecuteNonQuery(); 
				} 
				catch (SqlException objSQLException) 
				{ 
					strSQLExceptions += objSQLException.ToString() + "\r\n" + "\r\n"; 
				} 
				myConnection.Close(); 
			} 
			return strSQLExceptions; 
		}

		public int GetVersion() 
		{
			int Version = -1;

			// Create Instance of Connection and Command Object
			SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
			SqlCommand myCommand = new SqlCommand("StoreNuke_GetVersion", myConnection);

			// Mark the Command as a SPROC
			myCommand.CommandType = CommandType.StoredProcedure;

			// Execute the command
			myConnection.Open();

			try
			{
				SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

				// Return the datareader result
				if(result.Read())
				{
					Version = (int)result["Version"];
				}
				result.Close();
			}
			catch
			{
				//nothing
			}

			return Version;
		}

		public void AddVersion(int version) 
		{
			// Create Instance of Connection and Command Object
			SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
			SqlCommand myCommand = new SqlCommand("StoreNuke_AddVersion", myConnection);

			// Mark the Command as a SPROC
			myCommand.CommandType = CommandType.StoredProcedure;

			// Add Parameters to SPROC
			SqlParameter parameterVersion = new SqlParameter("@Version", SqlDbType.Int, 4);
			parameterVersion.Value = version;
			myCommand.Parameters.Add(parameterVersion);

			// Open the connection and execute the Command
			myConnection.Open();
			myCommand.ExecuteNonQuery();
			myConnection.Close();
		}

		public void UpdateGeneralAdministration(string defaultPageTitle, string defaultMetaDesc, string defaultMetaKey, bool sslEnabled, string logoPath, string gatewayPath, string defaultEmailAddress, string copyright) 
		{
			// Create Instance of Connection and Command Object
			SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
			SqlCommand myCommand = new SqlCommand("StoreNuke_UpdateGeneralAdministration", myConnection);

			// Mark the Command as a SPROC
			myCommand.CommandType = CommandType.StoredProcedure;

			// Add Parameters to SPROC

			SqlParameter parameterdefaultPageTitle = new SqlParameter("@defaultPageTitle", SqlDbType.NVarChar, 50);
			parameterdefaultPageTitle.Value = defaultPageTitle;
			myCommand.Parameters.Add(parameterdefaultPageTitle);

			SqlParameter parameterdefaultMetaDesc = new SqlParameter("@defaultMetaDesc", SqlDbType.NVarChar, 1000);
			parameterdefaultMetaDesc.Value = defaultMetaDesc;
			myCommand.Parameters.Add(parameterdefaultMetaDesc);

			SqlParameter parameterdefaultMetaKey = new SqlParameter("@defaultMetaKey", SqlDbType.NVarChar, 1000);
			parameterdefaultMetaKey.Value = defaultMetaKey;
			myCommand.Parameters.Add(parameterdefaultMetaKey);
	
			SqlParameter parametersslEnabled = new SqlParameter("@sslEnabled", SqlDbType.Bit, 1);
			parametersslEnabled.Value = sslEnabled;
			myCommand.Parameters.Add(parametersslEnabled);

			SqlParameter parameterlogoPath = new SqlParameter("@logoPath", SqlDbType.NVarChar, 100);
			parameterlogoPath.Value = logoPath;
			myCommand.Parameters.Add(parameterlogoPath);
	
			SqlParameter parametergatewayPath = new SqlParameter("@gatewayPath", SqlDbType.NVarChar, 100);
			parametergatewayPath.Value = gatewayPath;
			myCommand.Parameters.Add(parametergatewayPath);

			SqlParameter parameterdefaultEmailAddress = new SqlParameter("@defaultEmailAddress", SqlDbType.NVarChar, 50);
			parameterdefaultEmailAddress.Value = defaultEmailAddress;
			myCommand.Parameters.Add(parameterdefaultEmailAddress);

			SqlParameter parametercopyright = new SqlParameter("@copyright", SqlDbType.NVarChar, 50);
			parametercopyright.Value = copyright;
			myCommand.Parameters.Add(parametercopyright);

			// Open the connection and execute the Command
			myConnection.Open();
			myCommand.ExecuteNonQuery();
			myConnection.Close();
		}

		public void UpdateTerms(string terms) 
		{
			// Create Instance of Connection and Command Object
			SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
			SqlCommand myCommand = new SqlCommand("StoreNuke_UpdateTerms", myConnection);

			// Mark the Command as a SPROC
			myCommand.CommandType = CommandType.StoredProcedure;

			// Add Parameters to SPROC
	
			SqlParameter parameterterms = new SqlParameter("@terms", SqlDbType.Text);
			parameterterms.Value = terms;
			myCommand.Parameters.Add(parameterterms);

			// Open the connection and execute the Command
			myConnection.Open();
			myCommand.ExecuteNonQuery();
			myConnection.Close();
		}

		public void UpdatePrivacy(string privacy) 
		{
			// Create Instance of Connection and Command Object
			SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
			SqlCommand myCommand = new SqlCommand("StoreNuke_UpdatePrivacy", myConnection);

			// Mark the Command as a SPROC
			myCommand.CommandType = CommandType.StoredProcedure;

			// Add Parameters to SPROC
	
			SqlParameter parameterprivacy = new SqlParameter("@privacy", SqlDbType.Text);
			parameterprivacy.Value = privacy;
			myCommand.Parameters.Add(parameterprivacy);

			// Open the connection and execute the Command
			myConnection.Open();
			myCommand.ExecuteNonQuery();
			myConnection.Close();
		}

		public string GetPrivacy() 
		{
			// Create Instance of Connection and Command Object
			SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
			SqlCommand myCommand = new SqlCommand("StoreNuke_GetPrivacy", myConnection);

			// Mark the Command as a SPROC
			myCommand.CommandType = CommandType.StoredProcedure;

			// Execute the command
			myConnection.Open();
			SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

			string Privacy = "";

			// Return the datareader result
			if(result.Read())
			{
				Privacy = result["Privacy"].ToString();;
			}
			result.Close();

			return Privacy;
		}



		public string GetTerms() 
		{
			// Create Instance of Connection and Command Object
			SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
			SqlCommand myCommand = new SqlCommand("StoreNuke_GetTerms", myConnection);

			// Mark the Command as a SPROC
			myCommand.CommandType = CommandType.StoredProcedure;

			// Execute the command
			myConnection.Open();
			SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

			string Terms = "";

			// Return the datareader result
			if(result.Read())
			{
				Terms = result["Terms"].ToString();;
			}
			result.Close();

			return Terms;
		}

		public string GetCopyright() 
		{
			// Create Instance of Connection and Command Object
			SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
			SqlCommand myCommand = new SqlCommand("StoreNuke_GetCopyright", myConnection);

			// Mark the Command as a SPROC
			myCommand.CommandType = CommandType.StoredProcedure;

			// Execute the command
			myConnection.Open();
			SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

			string Copyright = "";

			// Return the datareader result
			if(result.Read())
			{
				Copyright = result["Copyright"].ToString();;
			}
			result.Close();

			return Copyright;
		}

		public SqlDataReader GetGeneralAdministration() 
		{
			// Create Instance of Connection and Command Object
			SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
			SqlCommand myCommand = new SqlCommand("StoreNuke_GetGeneralAdministration", myConnection);

			// Mark the Command as a SPROC
			myCommand.CommandType = CommandType.StoredProcedure;

			// Execute the command
			myConnection.Open();
			SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

			return result;
		}

		public string GetGatewayPath() 
		{

			// Create Instance of Connection and Command Object
			SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
			SqlCommand myCommand = new SqlCommand("StoreNuke_GetGatewayPath", myConnection);

			// Mark the Command as a SPROC
			myCommand.CommandType = CommandType.StoredProcedure;

			// Execute the command
			myConnection.Open();
			SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

			string gatewayPath = "";

			// Return the datareader result
			if(result.Read())
			{
				gatewayPath = result["GatewayPath"].ToString();
			}
			result.Close();

			return gatewayPath;
		}

	}
}
